package com.ari.controller;

import com.ari.pojo.Books;
import com.ari.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

/**
 * @author 19482
 */
@Controller
@RequestMapping("/book")
public class BookController {
    /**
     * constructor 调 service 层
     */
    @Autowired
    @Qualifier("bookServiceImpl")
    private BookService bookService;

    @RequestMapping("/allBook")
    public String list(Model model){
        List<Books> books = bookService.queryAllBooks();
        model.addAttribute("list",books);
        for (Books book: books) {
            System.out.println(book);
        }
        return "allBook";
    }
    /**
     *跳转到增加书籍页面
     */
    @RequestMapping("/toAddBook")
    public String toAddBook(){
    return "addBook";
    }
    //添加书籍
    @RequestMapping("/addBook")
    public String addBook(Books book){
        bookService.addBooks(book);
        return "redirect:/book/allBook";
    }
    //方法三：修改书籍信息
    @RequestMapping("/toUpdateBook")
    public String toUpdateBook(Model model,int id){
        Books book = bookService.queryBookById(id);
        model.addAttribute("book",book);
        System.out.println("book--"+ book);
        return "updateBook";
    }

    @RequestMapping("/updateBook")
    public String updateBook(Model model,Books book){
        bookService.updateBook(book);
        Books books = bookService.queryBookById(book.getBookId());
        model.addAttribute("books", books);
        System.out.println("修改后book--"+ book);
        return "redirect:/book/allBook";
    }
     //方法四：删除书籍【使用restFul风格】
    @RequestMapping("/del/{bookId}")
    public String deleteBook(@PathVariable("bookId") int id){
        bookService.deleteBookById(id);
        return "redirect:/book/allBook";
    }

}
